public class Test2 {
    public static void main(String[] args) {
        System.out.println(LastRemaining_Solution(5, 3));
        System.out.println();
    }
    public static int LastRemaining_Solution (int n, int m) {
    // write code here
    int tmp = 0;
    int sum = 0 ;
    for(int i = 0 ; i <= n-1 ; i++){
        sum += i ;
    }
    int n2 = n ;
    for(; n > 1 ; ){
        tmp = (tmp + m-1) % n2 ;
        sum -= tmp ;
        tmp = (tmp+1) % n2 ;
        n -- ;
    }
    return sum ;
}
}
